package se.webbzon.boltzmann.game.event;

import se.webbzon.boltzmann.effect.ShakeEffect;
import se.webbzon.boltzmann.timer.Timer;
import se.webbzon.boltzmann.timer.TimerListener;
import se.webbzon.oschi01.worldarea.WorldArea;
import se.webbzon.oschi01.worldobject.WorldObject;
import se.webbzon.oschi01.worldobject.WorldStaticObject;

public class ShakeEvent extends AbstractGameEvent {
	
	// The binding of this game event
	private final WorldObject binding;
	
	// The timer listener of this shake event
	private final TimerListenerImpl listener;
	
	/*============================================================
	Constructors
	============================================================*/

	public ShakeEvent(WorldObject binding) {
		this.binding = binding;
		listener = new TimerListenerImpl();
	}

	/*============================================================
	Public Methods
	============================================================*/
	
	@Override protected void onBegin(WorldArea world) {
		if (binding instanceof WorldStaticObject) {
			Timer t = new Timer(20);
			t.addListener(listener);
			new ShakeEffect((WorldStaticObject) binding, 20).launch(world);
			t.start(world);
		} else
			end();
	}

	@Override protected void onEnd() {};
	
	/*============================================================
	Private Classes
	============================================================*/
	
	private final class TimerListenerImpl implements TimerListener {

		@Override public void onAlarm(Timer timer, WorldArea world) {
			end();
		}
		
	}

}
